var app = getApp()
var Bmob = require('../../utils/Bmob-2.2.0.min.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[],
    franchiseeId:'',
    franchiseeInfo:{},
    categoryList: [{ category: 0, name: '全部' }],
    categoryActive: 0,
    form:'',
    
    getSectionData: {
      page: 0,
      loading: false,
      nomore: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    var formData=options.detail,
        that=this,
        franchiseeId = options.franchisee;
   
     var categoryActive = Number(options.type)||0;
  
    this.setData({
      
      franchiseeId: franchiseeId,
      categoryActive: categoryActive
    })

    console.log(franchiseeId);
    this.categoryList();
  },

  categoryList:function(){
    var that=this;
    var franchiseeId = that.data.franchiseeId;  

    var categoryList = Bmob.Query("categroyList");
   // var franchisee = Bmob.Pointer("Franchisee")
   // var poiID = franchisee.set(franchiseeId);
    categoryList.equalTo("franchisee","==", franchiseeId);
    categoryList.find().then(data=>{
    
        var categoryList = that.data.categoryList;
        for(var i=0;i<data.length;i++)
        {
          var category = { category: 0, name: '全部' }
          category.category = data[i].categroy;
          category.name=data[i].name;
          categoryList.push(category)
          }
        var data = {};
        data.categoryList = categoryList
        that.setData(data);
        


    })
 
    this.getGoods();
  },


  getGoods: function (){
    var that = this;
    var skip = that.data.getSectionData.page;
    var categoryActive = that.data.categoryActive;
    var franchiseeId = that.data.franchiseeId;
   
   
    var goods = Bmob.Query("goods");
    var franchisee = Bmob.Pointer("Franchisee")
    var poiID = franchisee.set(franchiseeId);

     var sdata = that.data.getSectionData;
    if (sdata.loading || sdata.nomore) {
      return;
    }

    goods.equalTo("FranchiseeId","==", poiID);
    if (categoryActive!=0)
     goods.equalTo("goods_model","==", categoryActive);
    goods.equalTo("state","!=", 0);
 
    goods.limit(10);
 
   
    goods.order('sales');  //排序

      goods.find().then(res=>{     
    
      
          var goods = [];
          for (var i = 0; i < res.length; i++) {

            var good = res[i];
          
            good.server_time = util.formatTime();
            good.downCount = {
              hours: '00',
              minutes: '00',
              seconds: '00'
            };
            if (good.seckill_start_state == 0) {
              that.downcount = app.beforeSeckillDownCount(good, that, 'goods_data[' + i + ']');
            } else if (good.seckill_start_state == 1) {
              that.downcount = app.duringSeckillDownCount(good, that, 'goods_data[' + i + ']');
            }
            var allstock = 0;
            for (var a = 0; a < good.models.length; a++) {
              var modelInfo = good.models[a];

              for (var j = 0; j < modelInfo.models.length; j++) {
                allstock += modelInfo.models[j].stock;

              }
            }
            good.seckill_progress = Math.round(good.sales / allstock);
            good.stock = allstock;
            good.seckill_progress = (good.sales / allstock * 100).toFixed(0);
            goods.push(good);
          }

          that.setData({
            goodsList: goods,
            'getSectionData.page': sdata.page + 10,
            'getSectionData.loading': false,
            'getSectionData.nomore': res.is_more == 0 ? true : false
          })
   
             
      });
    
 
  },

  turnToGoodsDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    let form = e.currentTarget.dataset.type;
    if(form==3){
      app.turnToPage('/pages/toStoreDetail/toStoreDetail?detail=' + id + '&franchisee=' + this.data.franchiseeId);
    }else{
    app.turnToPage('/pages/goodsDetail/goodsDetail?detail=' + id + '&franchisee=' + this.data.franchiseeId + '&cart_num=' + this.data.franchiseeInfo.cart_goods_num);
      console.log(e);
      console.log(form);
    }
  },


  clickOrderTab:function(e){
    var that=this;
    var index = e.currentTarget.dataset.id;
    this.setData({
      categoryActive: index,
      goodsList: [],
      getSectionData: {
        page: 1,
        loading: false,
        nomore: false
      }
    })
    this.getGoods();
  },
  scrollfunc : function(){
    var that = this;
    var skip = that.data.getSectionData.page;
    var categoryActive = that.data.categoryActive;
    var franchiseeId = that.data.franchiseeId;
    var goodsList=that.data.goodsList;
    var Good = Bmob.Object.extend("goods");

    var goods = new Bmob.Query(Good);
    var Franchisee = Bmob.Object.extend("Franchisee");
    var franchisee = new Franchisee();

    var sdata = that.data.getSectionData;
    if (sdata.loading || sdata.nomore) {
      return;
    }

    franchisee.id = franchiseeId;

    goods.equalTo("FranchiseeId", franchisee);
    if (categoryActive != 0)
      goods.equalTo("goods_model", categoryActive);
    goods.notEqualTo("state", 0);
    sdata.loading = true;
    goods.limit(10);

    goods.skip(skip);
    goods.descending('sales');  //排序

    goods.find({
      success: function (res) {
        //var goods=res;
    
       
        for (var i = 0; i < res.length; i++) {

          var good = res[i].attributes;
          good.id = res[i].id;
          good.server_time = util.formatTime();
          good.downCount = {
            hours: '00',
            minutes: '00',
            seconds: '00'
          };
          if (good.seckill_start_state == 0) {
            that.downcount = app.beforeSeckillDownCount(good, that, 'goods_data[' + i + ']');
          } else if (good.seckill_start_state == 1) {
            that.downcount = app.duringSeckillDownCount(good, that, 'goods_data[' + i + ']');
          }
          var allstock = 0;
          for (var a = 0; a < good.models.length; a++) {
            var modelInfo = good.models[a];

            for (var j = 0; j < modelInfo.models.length; j++) {
              allstock += modelInfo.models[j].stock;

            }
          }
          good.seckill_progress = Math.round(good.sales / allstock);
          good.stock = allstock;
          good.seckill_progress = (good.sales / allstock * 100).toFixed(0);
          goodsList.push(good);
        }

        that.setData({
          goodsList: goodsList,
          'getSectionData.page': sdata.page + 10,
          'getSectionData.loading': false,
          'getSectionData.nomore': res.is_more == 0 ? true : false
        })
      },
      fail: function (res) {
        that.setData({
          'getSectionData.loading': false
        });
      },
      complete: function () {

      }
    });


  }
 
})